package cn.com.justing.pigeon.feed;

import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.gnu.stealthp.rsslib.RSSChannel;
import org.gnu.stealthp.rsslib.RSSException;
import org.gnu.stealthp.rsslib.RSSHandler;
import org.gnu.stealthp.rsslib.RSSItem;
import org.gnu.stealthp.rsslib.RSSParser;

public class FeedFetcherImpl implements FeedFetcher
{
	private Date lastUpdatedDate = null;
	private URL feedUrl = null;
	private String feedString = null;
	private static Log log = LogFactory.getLog(FeedFetcherImpl.class);

	public FeedFetcherImpl(String feedString)
	{
		super();
		this.feedString = feedString;
		lastUpdatedDate = this.loadLastUpdatedTime(feedUrl);
	}

	public FeedFetcherImpl(URL feedUrl)
	{
		this.feedUrl = feedUrl;
		lastUpdatedDate = loadLastUpdatedTime(feedUrl);
	}

	@Override
	public List<RSSItem> getNewFeeds()
	{
		RSSHandler handler = new RSSHandler();
		List<RSSItem> newItemList = new ArrayList<RSSItem>();
		log.info("Trying to get RSS feeds");
		try
		{
			// RSSParser.parseXmlFile(feedUrl, handler, false);
			RSSParser.parseXmlFile(feedString, handler, false);

			RSSChannel channel = handler.getRSSChannel();
			LinkedList<RSSItem> list = channel.getItems();

			log.debug("Got feeds " + list.size());
			newItemList = findNewItems(list);

			log.debug("Got new feeds " + newItemList.size());
		} catch (RSSException e)
		{
			e.printStackTrace();
			log.debug(e);
		} catch (NullPointerException e)
		{
			e.printStackTrace();
			log.debug(e);
		}

		return newItemList;
	}

	List<RSSItem> findNewItems(LinkedList<RSSItem> list)
	{
		List<RSSItem> lst = new ArrayList<RSSItem>();
		int count = 0;
		String pubDateString = list.get(count).getPubDate();
		Date pubDate = new Date(Date.parse(pubDateString));
		Date newestDate = pubDate;
		boolean notAfter = lastUpdatedDate.before(pubDate);

		while (notAfter && count < list.size())
		{
			lst.add(list.get(count));
			count++;
			pubDateString = list.get(count).getPubDate();
			pubDate = new Date(Date.parse(pubDateString));

			notAfter = lastUpdatedDate.before(pubDate);
		}
		setUpdatedDate(newestDate);
		return lst;
	}

	Date getUpdatedDate()
	{
		return lastUpdatedDate;
	}

	void setUpdatedDate(Date updatedDate)
	{
		saveUpdateFile(feedUrl);
		this.lastUpdatedDate = updatedDate;
	}

	private void saveUpdateFile(URL feedUrl2)
	{

	}

	URL getFeedUrl()
	{
		return feedUrl;
	}

	void setFeedUrl(URL feedUrl)
	{
		this.feedUrl = feedUrl;
	}

	String getFeedString()
	{
		return feedString;
	}

	void setFeedString(String feedString)
	{
		this.feedString = feedString;
	}

	@Override
	public Date getUpdatedTime()
	{
		return lastUpdatedDate;
	}

	/**
	 * Load last updated time
	 * 
	 * @param feedUrl
	 * @return
	 */
	private Date loadLastUpdatedTime(URL feedUrl)
	{
		// TODO Auto-generated method stub
		return null;
	}

	public void setUrl(URL feedUrl)
	{
		this.feedUrl = feedUrl;
	}

}
